from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import unittest
from selenium.webdriver.chrome.options import Options

class Jiuxian(unittest.TestCase):
    def setUp(self) -> None:
        print('begin case')
    def tearDown(self) -> None:
        print('end case')
    def test_zheng2(self):
        options = Options()
        options.add_argument("--headless")  # 无头模式
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        driver = webdriver.Chrome(options=options)
        # driver=webdriver.Chrome()
        driver.get('https://www.jiuxian.com/')
        driver.maximize_window()
        #输入要添加的商品
        add=driver.find_element(By.ID,'wd')
        add.send_keys('五粮液')
        time.sleep(2)
        add.send_keys(Keys.BACK_SPACE)
        time.sleep(2)
        add.send_keys(Keys.CONTROL,'z')
        time.sleep(2)
        #enter键搜索
        add.send_keys(Keys.ENTER)
        # add.send_keys(Keys.SHIFT)
        time.sleep(3)
        #点击商品进入详情页
        driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
        driver.switch_to.window(driver.window_handles[-1])
        time.sleep(2)
        #断言商品名称和价格
        name=driver.find_element(By.XPATH,'/html/body/div[5]/div[1]/span').text
        print(name)
        self.assertEqual(name,'52°第八代五粮液 普五 500ml')
        price=driver.find_element(By.XPATH,'//*[@id="nowPrice"]/span[1]/strong').text
        print(price)
        # self.assertTrue(price=='929.00')
        #选择收货地址
        driver.find_element(By.CSS_SELECTOR,'.dIcon').click()
        driver.refresh()
        time.sleep(2)
        #选择商品数量
        driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[6]/div[5]/div[2]/div[1]/a[1]').click()
        time.sleep(2)
        #点击加入购物车
        driver.find_element(By.NAME,'detail_jiarugouwuche').click()
if __name__ == '__main__':
    #第一种执行方法
    unittest.main()
    #第二种执行方法
    unittest.TestLoader.loadTestsFromTestCase('Jiuxian')
    #第三种执行方法
    suite=unittest.TestSuite.addTest('test_zheng2')
    unittest.TextTestRunner.run(suite)
